'use strict';
var moment = require('moment');
exports = module.exports = function ($scope, $rootScope, $timeout, $state, $uibModal, yxtable14, $compile, $alert) {
    // 防止失误设计
    $scope.fangdai = '';
    $scope.fangdaiArr = [];
    // 自动读取
    $scope.splitDemo = `
        "bl":79,                //反检测比例
        "count":11,             //反检测数
        "BootCount":14,         //开机次数
        "Terminals":100,        //昨日终端数
        "UserId":10,            //用户id
        "user_name":"fjmtest8"  //用户名
    `;
    $scope.splitDemo1f = function(){
        var temp1arr = $scope.splitDemo.split('\n');
        var result = [];
        for(var i = 0, l = temp1arr.length; i < l; i++){
            // debugger;
            var one = temp1arr[i];
            var twoArr = one.match(/[\"\']([a-zA-Z0-0\_\-]+)[\"\']:[^,]+,?[ ]*\/\/(.*)/)
            if(twoArr)
            result.push({name: twoArr[2], title: twoArr[1], type:'文本', mock:'id'})
        }
        $scope.fromObj.tableColume = result;
    };
    (function(){
        try{
            var storage = window.localStorage;
            $scope.fangdaiArr = eval( '('+storage.getItem("makeTable14CodeV2")+')' );
            $scope.fangdaiArr = $scope.fangdaiArr || [];
        }catch(e){
            console.error(e)
        }
    })();
    $scope.setFromObj = (function() {
        for(var i = 0, l = $scope.fangdaiArr.length; i < l; i++){
            if ( $scope.fangdai == $scope.fangdaiArr[i].name ){
                $scope.fromObj = JSON.parse($scope.fangdaiArr[i].json);
                $scope.fangdai = '';
                return;
            }
        }
    });
    ///////////////////////////////
    $scope.fromObj = {
        listApi: {
            'url':'/api/Allot/IncomeB2TBList',
            'api':'IncomeB2TB',
            'breadcrumb': '一级目录,二级目录,代码生成逻辑',
            'pageSet': true
        },
        tab: [
            {title:'浏览器', link:'income.busi_brower',me:true},
            {title:'下载器', link:'income.busi_down'}
        ],
        searchBtn: [ // 搜索附加按钮
            {title:'同步数据', name:'synchronization', func:'确认'},
            {title:'导出', name:'export', func:'下载'},
            {title:'批量处理单价', name:'batchEditPrice', func:'弹框'},
            {title:'批量处理数量', name:'batchEditNumber', func:'弹框'},
            {title:'手动校准', name:'correct', func:'确认'},
            {title:'开启自动校准', name:'autoCorrect', func:'确认'}
        ],
        opBtn: [ // 操作
            {title:'调整', name:'adjustment', func:'弹框'},
            {title:'编辑', name:'eidt', func:'跳转',url:''},
            {title:'删除', name:'delete', func:'删除',api:'systemCustomer'},
            {title:'开启', name:'open', func:'改状态',api:'systemCustomer'}
        ],
        search: [
            {title:'begin|end',name:'时间段',type:'日期范围'},
            {title:'date',name:'日期',type:'日期'},
            {title:'keyWord',name:'关键字',type:'文本框'},
            {title:'username',name:'用户',type:'下拉框',dropSearch:true,api:'customer', url:'/api/customer/getlist'},
            {title:'bizId',name:'分类',type:'下拉框',dropSearch:true,api:'busiType', url:'/api/busi/typelist'},
            {title:'archive',name:'归档状态',type:'下拉框',api:'archiveStatus', staticData:`{'0':'未归档','1':'已归档'}`},
            {title:'splitState',name:'拆分状态',type:'下拉框',api:'splitStatus', staticData:`{'-1':'失败','1':'成功','0':'初始状态'}`}
        ],
        tableColume: [
            {title:'date',name:'产出日期',type:'文本',mock:'日期'},
            {title:'archiveDate',name:'归档日期',type:'文本',mock:'日期'},
            {title:'userName',name:'用户名',type:'文本',mock:'名字'},
            {title:'bizName',name:'业务名称',type:'文本',mock:'词语'},
            {title:'adId',name:'推广id',type:'文本',mock:'id'},
            {title:'url',name:'链接',type:'文本',mock:'网址'},
            {title:'uvCount',name:'UV数',type:'文本',mock:'数字',sum:true},
            {title:'uvAdjustCount',name:'调整后的UV数',type:'文本',mock:'数字',sum:true},
            {title:'price',name:'收入单价',type:'文本',mock:'金钱'},
            {title:'amount',name:'收入金额',type:'文本',mock:'金钱',sum:true},
            {title:'xxx',name:'收入总金额',type:'文本',mock:'金钱'},
            {title:'incomeType',name:'收入状态',type:'选择',option:`{'0':'预估','1':'暂定','2':'封账'}`,mock:'枚举3'},
            {title:'archiveState',name:'归档状态',type:'选择',option:`{'0':'未归档','1':'已归档'}`,mock:'枚举2'},
            {title:'splitState',name:'拆分状态',type:'选择',option:`{'-1':'失败','1':'成功','0':'初始状态'}`,mock:'枚举3'}
        ]
    };
    $scope.addItem = function(objName) {
        $scope.fromObj[objName].push({})
    };
    $scope.removeItem = function(objName, index) {
        $scope.fromObj[objName].splice(index, 1)
    };
    ///////////////////////////////
    var $API = {
        List : {},
        Drop : {},
        Detail : {},
        Delete : {},
        Edit : {}
    };
    var $ajax = {};
    $scope.produceCode = function() {
        var templateA = "";
        var templateAPI = "";
        var templateJ = "";
        var templateH = "";
        var json = $scope.fromObj;
        var randomId = Math.ceil(Math.random()*1e4);
        console.log( $scope.fromObj );
        (function() {
            templateA += `
                                                                                                    $ajax.sent = function(param) {
                                                                                                        return new Promise((resolve, reject) => {
                                                                                                            if(param.url == '${ json.listApi.url }') {
                                                                                                                var mock = {
                                                                                                                    "code": 200,
                                                                                                                    "msg":"",
                                                                                                                    "data": {
                                                                                                                        "list": [],
                                                                                                                        "rowcount": 0,
                                                                                                                        "sumDate": {}
                                                                                                                    }
                                                                                                                };
                                                                                                                var Random = Mock.Random;
                                                                                                                var rowcount = Random.integer(1, 100);
                                                                                                                var pagecount = ( rowcount > 10 ) ? 10 : rowcount;
                                                                                                                for(var i = 0, l = pagecount; i < l; i++) {
                                                                                                                    mock.data.list.push({`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var mock = one.mock.toLowerCase();
                if (mock == ('日期')) {
                    templateA += `
                                                                                                                        "${ title }": Random.date('yyyy-MM-dd'),`
                } else if (mock == ('名字')) {
                    templateA += `
                                                                                                                        "${ title }": Random.cname(),`
                } else if (mock == ('词语')) {
                    templateA += `
                                                                                                                        "${ title }": Random.cparagraph(1),`
                } else if (mock == ('id') || mock == ('数字')) {
                    templateA += `
                                                                                                                        "${ title }": Random.natural(1, 10000),`
                } else if (mock == ('网址')) {
                    templateA += `
                                                                                                                        "${ title }": Random.url(),`
                } else if (mock == ('枚举2')) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 1),`
                } else if (mock == ('枚举3')) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 2),`
                } else if (mock == ('枚举4')) {
                    templateA += `
                                                                                                                        "${ title }": Random.integer(0, 3),`
                } else if (mock == ('ip')) {
                    templateA += `
                                                                                                                        "${ title }": Random.ip(),`
                } else if (mock == ('金钱')) {
                    templateA += `
                                                                                                                        "${ title }": Random.float(1, 10000, 2, 2),`
                } else if (mock == ('邮箱')) {
                    templateA += `
                                                                                                                        "${ title }": Random.float(1, 10000, 2, 2),`
                } else if (mock == ('地址')) {
                    templateA += `
                                                                                                                        "${ title }": Random.email(),`
                } else { // 其他
                    templateA += `
                                                                                                                        "${ title }": Random.string('lower', 3, 10),`
                }
            }
            templateA = templateA.replace(/,$/, '');
            templateA += `
                                                                                                                    })
                                                                                                                }
                                                                                                                mock.data.rowcount = rowcount;
                                                                                                                (function() {
                                                                                                                    mock.data.sumDate = {`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                if(!one.sum) {continue;}
                json.tableSum = true;
                var title = one.title;
                var mock = one.mock.toLowerCase();
                if (mock == ('日期')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.date('yyyy-MM-dd'),`
                } else if (mock == ('名字')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.cname(),`
                } else if (mock == ('词语')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.cparagraph(1),`
                } else if (mock == ('id') || mock == ('数字')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.natural(1, 10000),`
                } else if (mock == ('网址')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.url(),`
                } else if (mock == ('枚举2')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 1),`
                } else if (mock == ('枚举3')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 2),`
                } else if (mock == ('枚举4')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.integer(0, 3),`
                } else if (mock == ('ip')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.ip(),`
                } else if (mock == ('金钱')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.float(1, 10000, 2, 2),`
                } else if (mock == ('邮箱')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.float(1, 10000, 2, 2),`
                } else if (mock == ('地址')) {
                    templateA += `
                                                                                                                        "${ title }Sum": Random.email(),`
                } else { // 其他
                    templateA += `
                                                                                                                        "${ title }Sum": Random.string('lower', 3, 10),`
                }
            }
            templateA = templateA.replace(/,$/, '');
            templateA += `
                                                                                                                    }
                                                                                                                })()
                                                                                                                resolve(mock);
                                                                                                                return
                                                                                                            }`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var type = one.type;
                var isSelect = type == ('下拉框')
                if( !isSelect ) {continue;}
                var url = one.url;
                var api = one.api;
                if(api && url) {
                    templateA += `
                                                                                                            if(param.url == '${ url }') {
                                                                                                                var mock = {
                                                                                                                    "code": 200,
                                                                                                                    "msg":"",
                                                                                                                    "data": {
                                                                                                                        "list": [
                                                                                                                            {"id": 11, "name": "假数据1"},
                                                                                                                            {"id": 21, "name": "假数据2"},
                                                                                                                            {"id": 31, "name": "假数据3"},
                                                                                                                            {"id": 41, "name": "假数据4"},
                                                                                                                            {"id": 51, "name": "假数据5"},
                                                                                                                            {"id": 61, "name": "假数据6"}
                                                                                                                        ]
                                                                                                                    }
                                                                                                                };
                                                                                                                resolve(select2Config(mock.data.list, 1));
                                                                                                                return;
                                                                                                            }`
                }
            }
            templateA += `
                                                                                                        });
                                                                                                    };
                                                                                                    $API.List.${ json.listApi.api } = function(params) {
                                                                                                        return new Promise(function(resolve, reject) {
                                                                                                            \/\/ 处理默认值 同时进行弥补处理`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        templateA += `
                                                                                                            params.${ ia[ii] } = params.${ ia[ii] };`
                    }
                } else {
                    templateA += `
                                                                                                            params.${ title } = params.${ title };`
                }
            }
            templateA += `
                                                                                                            $ajax.sent({
                                                                                                                method:'GET',
                                                                                                                url:'${ json.listApi.url }',
                                                                                                                params:params
                                                                                                            }).then(function(res) {
                                                                                                                resolve(res.data)
                                                                                                            }, reject);
                                                                                                        });
                                                                                                    }`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var type = one.type;
                var isSelect = type == ('下拉框')
                if( !isSelect ) {continue;}
                var name = one.name;
                var api = one.api;
                var url = one.url;
                templateA += `
                                                                                                    $API.Drop.${ api } = function(opt, params) {
                                                                                                        return new Promise(function(resolve, reject) {
                                                                                                            var nosearch = opt.indexOf('nosearch') > -1`
                if(api && url) {
                    templateA += `
                                                                                                            var noall = opt.indexOf('noall') > -1
                                                                                                            var arr = noall ? [{id: 0, name:"全部${ name }"}] :[];
                                                                                                            $ajax.sent({
                                                                                                                method: 'GET',
                                                                                                                url: '${ url }',
                                                                                                                params: {
                                                                                                                    // 传参
                                                                                                                }
                                                                                                            }).then(function(res) {
                                                                                                                resolve(select2Config( arr.concat(res.data||[]), !nosearch ))
                                                                                                            }, reject);`
                } else {
                    templateA += `
                                                                                                            var noall = opt.indexOf('noall') > -1
                                                                                                            var arr = noall ? [{id: 0, name:"全部${ title }"}] :[];
                                                                                                            var arr2 = [`
                    var staticData = '';
                    try{
                        staticData = eval('('+ one.staticData +')');
                    }catch(e) {
                        alert('error1036');
                        console.error("json格式有问题", one.staticData, e);
                    }
                    for(var ll in staticData) {
                        templateA += `
                                                                                                                {"id": ${ ll },"name":"${ staticData[ll] }" },`
                    }
                    templateA = templateA.replace(/,$/, '');
                    templateA += `
                                                                                                            ];
                                                                                                            resolve(select2Config( arr.concat(arr2), 0 ))`
                }
                templateA += `
                                                                                                        });
                                                                                                    };`
            }
            $("#templateA").html( templateA.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') );
            eval(templateA);
        })();
        (function() {
            templateJ += `
                                                                                                    // 搜索表单项
                                                                                                    $scope.obj = {`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var name = one.name;
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        templateJ += `
                                                                                                        '${ ia[ii] }': '', \/\/ 搜索项: ${ name }`
                    }
                } else {
                    templateJ += `
                                                                                                        '${ title }': '', \/\/ 搜索项: ${ name }`
                }
            }
            templateJ = templateJ.replace(/,$/, '');
            templateJ += `
                                                                                                    }
                                                                                                    \/\/ 初始化数据源
                                                                                                    $scope.dataSet = {`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var type = one.type;
                var name = one.name;
                var api = one.api;
                if (type == ('下拉框')) {
                    templateJ += `
                                                                                                        '${ api }': select2Config( [] ), \/\/ 数据源: ${ name }列表`
                }
            }
            templateJ = templateJ.replace(/,$/, '');
            templateJ += `
                                                                                                    }`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var type = one.type;
                var isSelect = type == ('下拉框')
                if( !isSelect ) {continue;}
                var name = one.name;
                var api = one.api;
                if(!api) {
                    alert('error1033');
                    console.error("未输入api in " + property);
                    continue;
                }
                templateJ += `
                                                                                                    \/\/ 读取${ name }列表
                                                                                                    $API.Drop.${ api }('all.search')
                                                                                                    .then(function(data) {
                                                                                                        $timeout(function() {
                                                                                                            $scope.dataSet.${ api } = data
                                                                                                        }, 0) }
                                                                                                    );`
            }
            templateJ += `
                                                                                                    \/\/ 表格数据
                                                                                                    $scope.yxt14loading = false; \/\/ 加载中`
            if( json.listApi.pageSet ){ //根据[是否分页]设置模板
                templateJ += `
                                                                                                    $scope.page = 1; \/\/ 分页
                                                                                                    $scope.pagesize = 30;//每页显示条数，默认30条;`
            }
            templateJ += `
                                                                                                    $scope.yxt14totalrow = 999; \/\/ 总条数
                                                                                                    $scope.yxt14datalist = [];
                                                                                                    $scope.yxt14datasum = {};
                                                                                                    \/\/ 读接口
                                                                                                    $scope.showList = function() {
                                                                                                        $scope.yxt14loading = true;
                                                                                                        var data = angular.extend({}, $scope.params, {
                                                                                                            pageIndex: $scope.page,
                                                                                                            pageSize: $scope.pagesize,
                                                                                                        })
                                                                                                        $API.List.${ json.listApi.api }(data).then(function(data) {
                                                                                                            $timeout(function() {
                                                                                                                $scope.yxt14datalist = data.list;
                                                                                                                $scope.yxt14totalrow = data.rowcount;
                                                                                                                $scope.yxt14datasum = data.sumDate;
                                                                                                                $scope.yxt14loading = false;
                                                                                                            }, 0)
                                                                                                        });
                                                                                                    }
                                                                                                    // 初始化
                                                                                                    yxtable14.init($scope);`
            for(var i = 0, l = json.opBtn.length; i < l; i++) {
                var one = json.opBtn[i];
                var title = one.title;
                var name = one.name;
                var func = one.func;
                templateJ += `
                                                                                                    $scope.${ name }Item = function(row) { \/\/ ${ title }方法
                                                                                                        \/\/ todo
                                                                                                        alert("这里应该是 ${ func } 处理对象 请尽快处理");`
                if( func == '弹框' ) {
                    templateJ += `
                                                                                                        $uibModal.open({
                                                                                                            templateUrl : '${ name }Item.html',
                                                                                                            controller : function($scope, $uibModalInstance, $timeout){
                                                                                                                $scope.frm = {
                                                                                                                    a1 : '',
                                                                                                                    a2 : '',
                                                                                                                    a3 : '',
                                                                                                                    a4 : ''
                                                                                                                };
                                                                                                                $scope.frm2 = {
                                                                                                                    nameSource : select2Config([])
                                                                                                                };
                                                                                                                $scope.submitFun = throttleSubmit(function(){
                                                                                                                    $scope.From.submitted = true;
                                                                                                                    if($scope.From.$invalid) { return false; }
                                                                                                                    return param
                                                                                                                },function(enable, param){
                                                                                                                    console.log('提交代码', param)
                                                                                                                    $timeout(function(){
                                                                                                                        alert("这里应该是提交操作逻辑");
                                                                                                                        $uibModalInstance.dismiss();
                                                                                                                        enable()
                                                                                                                    },3e3)
                                                                                                                })
                                                                                                                $scope.Cancel = function() {
                                                                                                                    $uibModalInstance.dismiss();
                                                                                                                };
                                                                                                            },
                                                                                                            windowClass: 'modalWidth700',
                                                                                                            scope:$scope.$new()
                                                                                                        });`
                } else if( func == '跳转' ) {
                    templateJ += `
                                                                                                        $go('app.make.maketable1', {id: row.id}) \/\/ ${ title }方法`
                } else if( func == '删除' ) {
                    var api = one.api;
                    templateJ += `
                                                                                                        $alert.confirm("是否删除本条数据", function(closeFn) {
                                                                                                            $API.Delete.${ api }Delete({id: row.id}) \/\/ ${ title }方法
                                                                                                            .then(function(data) {
                                                                                                                var result = data.res_code; \/\/ 0失败 1成功
                                                                                                                if( result == 1 ){
                                                                                                                    $alert.success(data.res||'${ title }成功');
                                                                                                                    $scope.showList();
                                                                                                                    closeFn(); // 关闭窗口
                                                                                                                    return false;
                                                                                                                }
                                                                                                                $alert.error(data.res||'${ title }失败')
                                                                                                            });
                                                                                                        })`
                } else if( func == '改状态' ) {
                    var api = one.api;
                    templateJ += `
                                                                                                        $API.Edit.${ api }Status({id: row.id}) \/\/ ${ title }方法
                                                                                                        .then(function(data) {
                                                                                                            var result = data.res_code; \/\/ 0失败 1成功
                                                                                                            if( result == 1 ){
                                                                                                                $alert.success(data.res||'${ title }成功')
                                                                                                                $timeout(function() {
                                                                                                                    \/\/ row.status = row.status == 0 ? 1 : 0;
                                                                                                                }, 0)
                                                                                                                return false;
                                                                                                            }
                                                                                                            $alert.error(data.res||'${ title }失败')
                                                                                                        });`
                }
                templateJ += `
                                                                                                    }`
            }
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var title = one.title;
                var name = one.name;
                var func = one.func;
                templateJ += `
                                                                                                    $scope.${ name }Item = function() { \/\/ ${ title }
                                                                                                        \/\/ TODO :: 这里应该是 ${ func }操作`
                if( func == '确认' ) {
                    templateJ += `
                                                                                                        $alert.confirm("是否进行${ title }", function(closeFn) {
                                                                                                            console.log("开始执行${ title }");
                                                                                                            closeFn(); \/\/ 关闭窗口
                                                                                                        })`
                } else if( func == '下载' ) {
                    templateJ += `
                                                                                                        var url = "http://www.baidu.com?t=" + Math.ceil(Math.random()*1e4);`
                    for(var ib = 0, lb = json.search.length; ib < lb; ib++) {
                        var one = json.search[ib];
                        var title = one.title;
                        var name = one.name;
                        if(title.indexOf("|") >-1) {
                            var ia = title.split("|");
                            for(var ii = 0, li = ia.length; ii < li; ii++) {
                                templateJ += `
                                                                                                        url += "&${ ia[ii] }=" + $scope.obj.${ ia[ii] }; \/\/ ${ name }`
                            }
                        } else {
                            templateJ += `
                                                                                                        url += "&${ title }=" + $scope.obj.${ title }; \/\/ ${ name }`
                        }
                    }
                    templateJ += `
                                                                                                        window.location.href = url`
                } else if( func == '弹框' ) {
                    templateJ += `
                                                                                                        $uibModal.open({
                                                                                                            templateUrl : '${ name }Item.html',
                                                                                                            controller : function($scope, $uibModalInstance, $timeout, throttleSubmit){
                                                                                                                $scope.frm = {
                                                                                                                    a1 : '',
                                                                                                                    a2 : '',
                                                                                                                    a3 : '',
                                                                                                                    a4 : ''
                                                                                                                };
                                                                                                                $scope.frm2 = {
                                                                                                                    nameSource : select2Config([])
                                                                                                                };
                                                                                                                $scope.submitFun = throttleSubmit(function(){
                                                                                                                    $scope.From.submitted = true;
                                                                                                                    if($scope.From.$invalid) { return false; }
                                                                                                                    var param = {};
                                                                                                                    return param
                                                                                                                },function(enable, param){
                                                                                                                    console.log('提交代码', param)
                                                                                                                    $timeout(function(){
                                                                                                                        alert("这里应该是提交操作逻辑");
                                                                                                                        $uibModalInstance.dismiss();
                                                                                                                        enable()
                                                                                                                    },3e3)
                                                                                                                })
                                                                                                                $scope.Cancel = function() {
                                                                                                                    $uibModalInstance.dismiss();
                                                                                                                };
                                                                                                            },
                                                                                                            windowClass: 'modalWidth700',
                                                                                                            scope:$scope.$new()
                                                                                                        });`
                }
                templateJ += `
                                                                                                    }`
            }
            $("#templateJ").html(
                `&lt;script type="text/javascript"&gt;\napp.controller("controller${ randomId }", function($scope, $rootScope, $timeout, $go, $alert, $state, $uibModal, yxtable14, $ajax, $API) {`+
                templateJ.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n')+
                '\n});\n&lt;/script&gt;'
            );
            eval(templateJ);
        })();
        (function() {
            var breadcrumb = $scope.fromObj.listApi.breadcrumb.replace(/,/g, `</a><a href="javascript:;">`);
            templateH += `
                                                                                                    <div class="yxt14breadcrumb">
                                                                                                        <a href="javascript:;">当前位置 : ${ breadcrumb }</a>`
            var title_last = $scope.fromObj.listApi.breadcrumb.replace(/.+,/g, '');
            templateH += `
                                                                                                    </div>
                                                                                                    <div class="container-fluid">
                                                                                                        <div class="yxt14main panel">
                                                                                                            <div class="yxt14title"><i></i>&nbsp;&nbsp;${ title_last }</div>
                                                                                                            <div class="yxt14nav">`
            for(var i = 0, l = json.tab.length; i < l; i++) {
                var one = json.tab[i];
                var title = one.title;
                var link = one.link;
                var me = one.me?'yxt14active':'';
                templateH += `
                                                                                                                <a class="yxt14navson ${ me }" ng-click="goto('${ link }')">${ title }</a>`
            }
            templateH += `
                                                                                                            </div>
                                                                                                            <form class="form-inline">`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var type = one.type;
                var name = one.name;
                templateH += `
                                                                                                                <div class="form-group">`
                if (type == ('下拉框')) {
                    var api = one.api;
                    if (!api) {
                        alert('error1036');
                        console.error("未输入api in " + property);
                    }
                    templateH += `
                                                                                                                    <input select2 config="dataSet.${ api }" ng-model="obj.${ title }"
                                                                                                                        select2-model='dataSet.${ title }' class="form-control" placeholder="全部${ name }"
                                                                                                                    />`
                } else if (type == ('文本框')) {
                    templateH += `
                                                                                                                    <input class="form-control" type="text" name="${ title }"
                                                                                                                        ng-model="obj.${ title }" placeholder="请输入${ name }"
                                                                                                                    />`
                } else if (type == ('日期')) {
                    templateH += `
                                                                                                                    <input datetime-picker type="text" ng-model="obj.${ title }" class="form-control"
                                                                                                                        config='{"format":"yyyy-mm-dd","startView":"2","minView":"2","todayBtn":false}' placeholder="请选择${ name }"
                                                                                                                    />`
                } else if (type == ('日期范围')) {
                    var iarr = title.split('|');
                    templateH += `
                                                                                                                    <le-date style="width:200px;" bdate="obj.${ iarr[0] }" edate="obj.${ iarr[1] }"
                                                                                                                    ></le-date>`
                }
                templateH += `
                                                                                                                </div>`
            }
            templateH += `
                                                                                                                <div class="form-group">
                                                                                                                    <button ng-click="search()" class="btn yxt14btn-red">搜&nbsp;&nbsp;索</button>
                                                                                                                </div>
                                                                                                                <div class="form-group">
                                                                                                                    <button ng-click="reset()" class="btn yxt14btn-gray">重&nbsp;&nbsp;置</button>
                                                                                                                </div>`
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var title = one.title;
                var name = one.name;
                templateH += `
                                                                                                                <div class="form-group pull-right">
                                                                                                                    <button class="btn yxt14btn-blue" ng-click="${ name }Item()">
                                                                                                                        <i class="glyphicon glyphicon-plus"></i>&nbsp;${ title }
                                                                                                                    </button>
                                                                                                                </div>`
            }
            templateH += `
                                                                                                                <div style="clear:both;"></div>
                                                                                                            </form>
                                                                                                            <div class="table-responsive main-table-striped">
                                                                                                                <table class="table">
                                                                                                                <thead>
                                                                                                                    <tr>`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var name = one.name;
                templateH += `
                                                                                                                        <th class="yxt14left" title="${ name }" style="width:100px;">${ name }</th>`
            }
            if (json.opBtn.length) {
                templateH += `
                                                                                                                        <th class="yxt14center" title="操作" style="width:100px;">操作</th>`
            }
            templateH += `
                                                                                                                    </tr>
                                                                                                                </thead>
                                                                                                                <tbody ng-show="!yxt14datalist.length && !yxt14loading">
                                                                                                                    <tr>
                                                                                                                        <td colspan="99"><center>无数据</center></td>
                                                                                                                    </tr>
                                                                                                                </tbody>
                                                                                                                <tbody>
                                                                                                                    <tr ng-repeat="row in yxt14datalist">`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var type = one.type;
                var switche = type == ('选择');
                if (switche) {
                    var api = one.option;
                    templateH += `
                                                                                                                        <td class="yxt14center" title="{{ row.${ title } }}">
                                                                                                                            <div class="yxt14ellipsis">
                                                                                                                                {{ ${ api }[row.${ title }]||'-' }}
                                                                                                                            </div>
                                                                                                                        </td>`
                } else { // 其他的都认为是文本
                    templateH += `
                                                                                                                        <td class="yxt14left" title="{{ row.${ title } }}">
                                                                                                                            <div class="yxt14ellipsis">
                                                                                                                                {{ row.${ title } }}
                                                                                                                            </div>
                                                                                                                        </td>`
                }
            }
            if (json.opBtn.length) {
                templateH += `
                                                                                                                        <td class="yxt14center">`
                for(var i = 0, l = json.opBtn.length; i < l; i++) {
                    var one = json.opBtn[i];
                    var name = one.name;
                    var title = one.title;
                    templateH += `
                                                                                                                            <a ng-click="${ name }Item(row)" style="margin-right:10px">${ title }</a>`
                }
                templateH += `
                                                                                                                        </td>`
            }
            templateH += `
                                                                                                                    </tr>
                                                                                                                </tbody>`
            if(json.tableSum) {
                templateH += `
                                                                                                                <tbody class="tablefoot">
                                                                                                                    <tr>`
                for(var i = 0, l = json.tableColume.length; i < l; i++) {
                    var one = json.tableColume[i];
                    var title = one.title;
                    if(i==0) {
                        templateH += `
                                                                                                                        <td class="yxt14left"> <b>汇总</b> </td>`
                    } else if ( !one.sum ) {
                        templateH += `
                                                                                                                        <td></td>`
                    } else {
                        templateH += `
                                                                                                                        <td class="yxt14left" title="{{ yxt14datasum.${ title }Sum }}">
                                                                                                                            <b>{{ yxt14datasum.${ title }Sum }}</b>
                                                                                                                        </td>`
                    }
                }
                if (json.opBtn.length) {
                    templateH += `
                                                                                                                        <td></td>`
                }
                templateH += `
                                                                                                                    </tr>
                                                                                                                </tbody>`
            }
            templateH += `
                                                                                                                <tbody ng-show="yxt14loading">
                                                                                                                    <tr class="yxt14loading">
                                                                                                                        <td colspan="99">
                                                                                                                            <center>数据加载中……</center>
                                                                                                                            <div> <div class="sk-spinner sk-spinner-wave"> <div class="sk-rect1"></div> <div class="sk-rect2"></div>
                                                                                                                            <div class="sk-rect3"></div> <div class="sk-rect4"></div> <div class="sk-rect5"></div> </div> </div>
                                                                                                                        </td>
                                                                                                                    </tr>
                                                                                                                </tbody>
                                                                                                                </table>
                                                                                                            </div>`
            if( json.listApi.pageSet ){ //根据[是否分页]设置模板
                templateH += `
                                                                                                            <div class="main-table-info">
                                                                                                                <div class="main-table-info-msg"> <h6>{{showMessage()}}</h6> </div>
                                                                                                                <div class="pull-right">
                                                                                                                    <uib-pagination total-items="yxt14totalrow" ng-model="page" max-size="3"
                                                                                                                        first-text="第一页" items-per-page="pagesize" ng-change="pageChanged()"
                                                                                                                        previous-text="上一页" next-text="下一页" last-text="最后页"
                                                                                                                        boundary-links="true" boundary-link-numbers="true">
                                                                                                                    </uib-pagination>
                                                                                                                </div>
                                                                                                            </div>`
            }
            templateH += `
                                                                                                        </div>`
            for(var i = 0, l = json.searchBtn.length; i < l; i++) {
                var one = json.searchBtn[i];
                var name = one.name;
                var func = one.func;
                if( func == '弹框' ) {
                    templateH += `

                                                                                                        <script type="text/ng-template" id="${ name }Item.html" >
                                                                                                            <form class="yxf14" name="From">
                                                                                                                <b class="yxf14-title">重新整理的表单</b>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">输入框</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <input type="text" placeholder="请输入标题" style="width:200px;"
                                                                                                                            ng-model="frm.a1" name="a1" maxlength="21"
                                                                                                                            required ng-minlength="4" ng-maxlength="20">
                                                                                                                        <b ng-show="( From.a1.$dirty || From.submitted ) && From.a1.$invalid"> {{From.a1.$error.required?"必填项":"请输入4-20字符"}} </b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">下拉框</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <input type="text" select2 style="width:200px;" name="a2"
                                                                                                                            ng-model="frm.a2" config="obj2.nameSource" select2-model="frm2.a2" required
                                                                                                                        />
                                                                                                                        <b ng-show="( From.a2.$dirty || From.submitted ) && From.a2.$invalid"> 必填项 </b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">领取方式</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a3" convert-to-number value="0"> <i></i>线下核销</label>
                                                                                                                        <label class="checkbox-inline i-checks"> <input type="radio" ng-model="frm.a3" convert-to-number value="1"> <i></i>邮寄</label>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="yxf14-box">
                                                                                                                    <label class="yxf14-label must">简介</label>
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <textarea placeholder="请输入简介" style="width:400px;"
                                                                                                                            ng-model="frm.a4" name="a4" rows="3" maxlength="64" required>
                                                                                                                        </textarea>
                                                                                                                        <i ng-hide="( From.a4.$dirty || From.submitted ) && From.a4.$invalid">{{ frm.a4.length || 0 }} / 64</i>
                                                                                                                        <b ng-show="( From.a4.$dirty || From.submitted ) && From.a4.$invalid"> 必填项</b>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <!-- 提交 -->
                                                                                                                <div class="yxf14-box">
                                                                                                                    <div class="yxf14-control">
                                                                                                                        <button type="button" class="btn btn-danger" ng-click="submitFun($event)">提交表单</button>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                            </form>
                                                                                                        </script>`
                }
            }
            templateH += `
                                                                                                    </div>`
            var dom = $compile(templateH)($scope);
            $("#jsonMakeTable").html(dom);
            $("#templateH").html(
                `&lt;div class="yxt14" ng-controller='controller${ randomId }'&gt;`+
                templateH.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') +
                '\n&lt;/div&gt;'
             );
        })();
        (function() {
            templateAPI += `
                                                                                                    {
                                                                                                        "url":"${ json.listApi.url }",
                                                                                                        "method": "GET",
                                                                                                        "params":{`
            for(var i = 0, l = json.search.length; i < l; i++) {
                var one = json.search[i];
                var title = one.title;
                var name = one.name;
                var type = one.type;
                var api = one.api;
                if(title.indexOf("|")>-1) {
                    var ia = title.split("|");
                    for(var ii = 0, li = ia.length; ii < li; ii++) {
                        templateAPI += `
                                                                                                            ${ ia[ii] } : '', \/\/ ${ name } | ${ type } | ${ api }`
                    }
                } else {
                    templateAPI += `
                                                                                                            ${ title } : '', \/\/ ${ name } | ${ type } | ${ api }`
                }
            }
            templateAPI += `
                                                                                                        },
                                                                                                        "response":{
                                                                                                            "code": "200",
                                                                                                            "msg": "",
                                                                                                            "data":{
                                                                                                                "list":[{`
            for(var i = 0, l = json.tableColume.length; i < l; i++) {
                var one = json.tableColume[i];
                var title = one.title;
                var name = one.name;
                var type = one.type;
                var mock = one.mock;
                templateAPI += `
                                                                                                                    "${ title }": "", \/\/ ${ name } | ${ type } | ${ mock }`
            }
            templateAPI += `
                                                                                                                }],
                                                                                                                "rowcount":100 //条数
                                                                                                            }
                                                                                                        }
                                                                                                    }`
            $("#templateAPI").html( templateAPI.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{96}/g, '\n') );
        })();
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var str = JSON.stringify($scope.fromObj);
        $scope.fangdaiArr.push({
            name: moment().format('YYYYMMDDHHmmss'),
            json: str
        })
        var storage = window.localStorage;
        storage.setItem("makeTable14CodeV2", JSON.stringify($scope.fangdaiArr) );
    };
    $scope.makeCodeFile = function() {
        $.ajax({
            url: 'http://localhost:4010/makeJsonFile',
            method: "post",
            data: {
                codea: templateA.replace(/\n[ ]{72}/g, '\n'),
                codej: templateJ.replace(/\n[ ]{72}/g, '\n'),
                codeh: templateH.replace(/\n[ ]{76}/g, '\n')
            }
        });
    };
}
